<template>
	<view style=" min-height:100vh;background-color: white;padding: 20rpx;padding-bottom: 40rpx;">
		<fui-form ref="form" labelWidth="180">
			<fui-form-item asterisk bottomBorder label="标题:">
				<fui-input :borderBottom="false" :padding="[0]" placeholder="请输入标题"></fui-input>
			</fui-form-item>

			<fui-form-item asterisk bottomBorder label="图片:">
				<template v-slot:vertical>
					<view class="">
						<view style="width: 100%;">
							<fui-upload immediate :url="url" ref="imgUpload" @success="success" @error="error"
								@complete="complete"></fui-upload>
						</view>

					</view>
				</template>
			</fui-form-item>
			<fui-form-item asterisk bottomBorder label="添加规格:">
				<template v-slot:vertical>
					<view class="flex" style="padding: 10rpx 0rpx;flex-wrap: wrap;">
						<view class="guigeBox" v-for="(item,index) in guigeList" :key="index"
							@click="clickGuige(item,index)">
							{{item.name}}
						</view>
						<view class="guigeBox" @click="plusGuige">
							<fui-icon name="plus" size="28"></fui-icon>
						</view>
					</view>
				</template>
			</fui-form-item>
			<fui-form-item :padding='["30rpx",0]' bottomBorder label="详情描述:">
				<template v-slot:vertical>
					<fui-textarea maxlength="-1" :padding="['32rpx','0rpx']" :border-bottom="false" :border-top="false"
						placeholder="请输入详细描述..." v-model="formData.descr"></fui-textarea>
				</template>
			</fui-form-item>
			<fui-form-item asterisk bottomBorder label="是否有转发奖励:">
				<fui-radio-group name="radio" @change="checked">
					<view class="fui-list__item flex">

						<view class="fui-align__center">
							<fui-radio value="0" :checked="gender==0"></fui-radio>
							<text class="fui-text">没有</text>
						</view>


						<view class="fui-align__center">
							<fui-radio value="1" :checked="gender==1"></fui-radio>
							<text class="fui-text">有</text>
						</view>

					</view>
				</fui-radio-group>

			</fui-form-item>
			<fui-form-item asterisk bottomBorder label="转发奖励:" v-if="gender==1">
				<view class="flex" style="justify-content: space-between;">
					<view class="flex" style="width: 48%;">
						<fui-input :borderBottom="false" :padding="['20rpx','0rpx']" type="number"
							placeholder="请输入"><text>币/浏览</text></fui-input>
					</view>
					<view class="flex" style="width: 48%;">
						<fui-input :borderBottom="false" :padding="['20rpx','0rpx']" type="number"
							placeholder="请输入"><text>币/成交</text></fui-input>
					</view>
				</view>
			</fui-form-item>
		</fui-form>
		<view class="btnBox">
			<fui-button text="确定发布" @click="submit"></fui-button>
		</view>
		<fui-bottom-popup :show="show" @close="closePopup" :maskClosable="false">
			<view class="fui-popup__container">
				<text class="fui-title">{{isEdit?'编辑规格':'添加规格'}}</text>
				<view class="fui-btn__box">
					<fui-form ref="guigeForm" labelWidth="180">
						<fui-form-item asterisk bottomBorder label="规格名:">
							<fui-input v-model="guigMsg.name" :borderBottom="false" :padding="[0]"
								placeholder="请输入规格名"></fui-input>
						</fui-form-item>
						<fui-form-item asterisk bottomBorder label="原价:">
							<fui-input v-model="guigMsg.oldPrice" :borderBottom="false" :padding="[0]"
								placeholder="请输入原价"></fui-input>
						</fui-form-item>
						<fui-form-item asterisk bottomBorder label="促销价:">
							<fui-input v-model="guigMsg.newPrice" :borderBottom="false" :padding="[0]"
								placeholder="请输入促销价"></fui-input>
						</fui-form-item>
						<fui-form-item asterisk bottomBorder label="销售限量:">
							<fui-input v-model="guigMsg.limited" :borderBottom="false" :padding="[0]"
								placeholder="请输入销售限量"></fui-input>
						</fui-form-item>
						<fui-form-item asterisk bottomBorder label="每人限购:">
							<fui-input v-model="guigMsg.bought" :borderBottom="false" :padding="[0]"
								placeholder="请输入每人限购"></fui-input>
						</fui-form-item>
					</fui-form>
					<view class="" style="padding-top: 20rpx;">
						<view class="flex" style="width: 100%;justify-content: space-around;" v-if="isEdit">
							<fui-button btnSize="small" text="修改" type="primary" @click="editGuige"></fui-button>
							<fui-button btnSize="small" text="删除" type="warning" @click="delGuige"></fui-button>
						</view>
						<view class="flex" style="width: 100%;justify-content: center;" v-else>
							<fui-button btnSize="small" text="添加" type="success" @click="addGuige"></fui-button>
						</view>

					</view>
				</view>
				<view class="fui-icon__close" @tap="closePopup">
					<fui-icon name="close" :size="48"></fui-icon>
				</view>
			</view>
		</fui-bottom-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				guigeIndex: null,
				isEdit: false,
				guigMsg: {
					name: '',
					oldPrice: '',
					newPrice: '',
					limited: 0,
					bought: 0,
				},
				guigeList: [{
					name: '第一规格',
					oldPrice: '100',
					newPrice: '80',
					limited: 10,
					bought: 1,
				}],
				guigeForm: null,
				show: false,
				//上传接口地址
				url: 'https://ffa.firstui.cn/api/example/upload-file',
				//上传状态，用于保存或其他操作时做判断
				status: '',
				//上传的视频地址列表
				imgUrls: [],
				videoUrls: [],
				form: null,
				imgUpload: null,
				videoUpload: null,
				formData: {},
				gender: 0,
			};
		},
		methods: {
			// 删除规格
			delGuige() {
				this.guigeList.splice(this.guigeIndex, 1)
				this.closePopup()
			},
			// 修改规格
			editGuige() {
				this.guigeList[this.guigeIndex] = this.guigMsg
				this.closePopup()
			},
			// 点击的那个规格
			clickGuige(item, index) {
				this.isEdit = true
				this.show = true
				this.guigMsg = Object.assign({}, item)
				this.guigeIndex = index
			},
			// 添加规格
			addGuige() {
				this.guigeList.push(this.guigMsg)
				this.guigMsg = {
					name: '',
					oldPrice: '',
					newPrice: '',
					limited: 0,
					bought: 0,
				}
			},
			// 数据初始化
			closePopup() {
				this.guigeIndex = null
				this.show = false
				this.isEdit = false
				this.guigMsg = {
					name: '',
					oldPrice: '',
					newPrice: '',
					limited: 0,
					bought: 0,
				}
			},
			plusGuige() {
				this.show = true
			},
			//上传成功时触发
			success(e) {
				console.log(e)
				//e.res 为服务器返回数据
				//上传成功回调，处理服务器返回数据【此处根据实际返回数据进行处理】
				let res = JSON.parse(e.res.data.replace(/\ufeff/g, "") || "{}")
				this.status = e.status
				if (res.data.url) {
					//处理结果返回给组件 
					//res.data.url为上传成功后返回的视频地址
					//e.index为视频索引值
					// 0是图片 1是视频
					if (this.gender == 0) {
						this.$refs.imgUpload.result(res.data.url, e.index)
					}
					if (this.gender == 1) {
						this.$refs.videoUpload.result(res.data.url, e.index)
					}

				}
			},
			//上传失败时触发
			error(e) {
				this.status = e.status
			},
			//选择视频或上传完成时触发
			complete(e) {
				this.status = e.status
				// 0是图片1 是视频
				if (this.gender == 0) {
					this.imgUrls = e.urls
				}
				if (this.gender == 1) {
					this.videoUrls = e.urls
				}

				if (this.status === 'success' && e.action === 'upload') {
					// this.fui.toast('上传完成！')
					//已上传完成的视频列表 this.urls
					console.log(this.urls)
				}
			},
			checked(e) {
				this.gender = e.detail.value

			},
			onFormSubmit(e) {
				this.formData = e.detail.value
				console.log('this.formData', this.formData);

			},
			onFormReset(e) {
				this.formData = {}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-align__center {
		margin-right: 20rpx;
		display: flex;
		align-items: center;
	}

	.fui-popup__container {
		width: 100%;
		position: relative;
		display: flex;
		justify-content: center;
		flex-direction: column;
		padding: 24rpx 32rpx;
		box-sizing: border-box;
	}

	.fui-title {
		font-size: 30rpx;
		font-weight: bold;
		text-align: center;
	}

	.fui-sub__title {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		text-align: center;
		font-size: 24rpx;
		color: #7F7F7F;
		transform: scale(.9);
	}

	.fui-descr {
		font-weight: bold;
		padding-top: 64rpx;
	}

	.fui-sub__descr {
		font-size: 26rpx;
		color: #B2B2B2;
		padding: 32rpx 0;
	}

	.fui-btn__box {

		padding: 32rpx 0;
		box-sizing: border-box;
	}

	.fui-icon__close {
		position: absolute;
		top: 24rpx;
		left: 24rpx;
	}

	.guigeBox {
		font-size: 28rpx;
		padding: 10rpx 20rpx;
		background-color: #F8F8F8;
		border-radius: 20rpx;
		margin-right: 10rpx;
		margin-bottom: 10rpx;
	}

	.fui-text {
		margin-left: 10rpx;
	}

	::v-deep .fui-uploadv__del-wrap {
		width: 100% !important;
		margin: 0 0 0 0 !important;
	}

	::v-deep .fui-uploadv__item {
		width: 100% !important;
		margin: 0 0 0 0 !important;
	}

	::v-deep .fui-upload__video {
		width: 100% !important;
		margin: 0 0 0 0 !important;
	}

	.uni-form-item {
		padding: 15px 0;
	}

	.title {
		margin-bottom: 10px;
	}

	.group-item {
		flex-direction: row;
		margin-right: 20px;
	}

	.btn {
		flex: 1;
	}

	.btn-submit {
		margin-right: 5px;
	}

	.btn-reset {
		margin-left: 5px;
	}

	.result {
		margin-top: 30px;
	}

	.formItem {
		background-color: white;
		padding: 20rpx;
	}

	.btnBox {
		margin-top: 20rpx;

	}
</style>